# -*- coding: utf-8 -*-
# @Author	: changfeng
# @Date		: 2025/9/13 14:11
# @Last Modified by:   changfeng
# @Last Modified time: 2025/9/13 14:11
# Thanks for your comments!
import datetime

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger

from src.basedata.application.commands import SyncBasedata
from src.basedata.application.timer_task_handler import sync_basedata_handler
from src.basedata.infrastructure.common.unit_of_work import AbstractUnitOfWork
from src.config import CONF


async def register_sync_basedata_from_his(scheduler: AsyncIOScheduler, uow: AbstractUnitOfWork):
    timer_sync_corn = CONF.timer.get('sync_basedata_corn')
    if CONF.server_start_sync_data:
        scheduler.add_job(func=sync_basedata_handler,
                          trigger=CronTrigger.from_crontab(timer_sync_corn),
                          id='job-sflz-sync_his_basedata', kwargs={'cmd': SyncBasedata(), 'uow': uow},
                          next_run_time=datetime.datetime.now())
    else:
        scheduler.add_job(func=sync_basedata_handler,
                          trigger=CronTrigger.from_crontab(timer_sync_corn),
                          id='job-sflz-sync_his_basedata', kwargs={'cmd': SyncBasedata(), 'uow': uow})
    # scheduler.add_job(func=sync_basedata_handler, trigger="interval", seconds=20,
    #                   id='job-not-checkin', kwargs={'cmd': SyncBasedata(), 'uow': uow},
    #                   next_run_time=datetime.datetime.now())


async def register_timer_task(uow: AbstractUnitOfWork):
    scheduler = AsyncIOScheduler()
    await register_sync_basedata_from_his(scheduler, uow)
    scheduler.start()
